package com.yidu.hbatsams.action.emp;

import com.yidu.hbatsams.utils.ExportWordTools.XWPFHelper;
import com.yidu.hbatsams.utils.ExportWordTools.XWPFHelperTable;
import com.yidu.hbatsams.utils.ExportWordTools.XWPFHelper;
import com.yidu.hbatsams.utils.ExportWordTools.XWPFHelperTable;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * @描述：人员管理导出word文档样式类
 * @ClassName: wenjian
 * @backage: PACKAGE_NAME
 * @Date: 2019/11/29 9:08
 * @Author:张超
 **/
public class EmpExportWord {
    public int type = 0;
    private XWPFHelperTable xwpfHelperTable = null;
    private XWPFHelper xwpfHelper = null;

    public EmpExportWord(int type) {
        this.type = type;
        xwpfHelperTable = new XWPFHelperTable();
        xwpfHelper = new XWPFHelper();
    }

    public EmpExportWord() {
        xwpfHelperTable = new XWPFHelperTable();
        xwpfHelper = new XWPFHelper();
    }
    /**
     * 创建好文档的基本 标题，表格  段落等部分
     * @return
     * @Author Huangxiaocong 2018年12月16日
     */
    public XWPFDocument createXWPFDocument() {
        XWPFDocument doc = new XWPFDocument();
        createTitleParagraph(doc);
        if(type==1){
            createTableParagraph(doc, 21, 6);
        }else if(type==2||type==3||type==4){
            createTableParagraph(doc, 7, 6);
        }else if(type==5){
            createTableParagraph(doc, 4, 6);
        }


        return doc;
    }
    /**
     * 创建表格的标题样式
     * @param document
     * @Author Huangxiaocong 2018年12月16日 下午5:28:38
     */
    public void createTitleParagraph(XWPFDocument document) {
        XWPFParagraph titleParagraph = document.createParagraph();    //新建一个标题段落对象（就是一段文字）
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);//样式居中
        XWPFRun titleFun = titleParagraph.createRun();    //创建文本对象

        //titleFun.setText(titleName); //设置标题的名字
        titleFun.setBold(true); //加粗
        titleFun.setColor("000000");//设置颜色
        titleFun.setFontSize(22);    //字体大小
        titleFun.setFontFamily("方正小标宋简体");//设置字体
        //titleFun.addBreak();    //换行  当前行切到下一行
    }
    /**
     * 设置表格格式
     * @param document
     * @param rows
     * @param cols
     * @Author Huangxiaocong 2018年12月16日
     */
    public void createTableParagraph(XWPFDocument document, int rows, int cols) {
        //xwpfHelperTable.createTable(xdoc, rowSize, cellSize, isSetColWidth, colWidths)

        //新建一个表格 名字为infoTable
        XWPFTable infoTable = document.createTable(rows, cols);
        xwpfHelperTable.setTableWidthAndHAlign(infoTable, "76000", STJc.CENTER);


        //设置指定宽度
        CTTbl ttbl = infoTable.getCTTbl();
        CTTblGrid tblGrid = ttbl.addNewTblGrid();
        int[] colWidths = new int[] {800,800,800,600,600,600};
        for (int i : colWidths) {
            CTTblGridCol gridCol = tblGrid.addNewGridCol();
            gridCol.setW(new BigInteger(i+""));
        }

        if(type==1){
            for(int col = 0; col < 21; col++){
                if(col>6){
                    xwpfHelperTable.mergeCellsHorizontal(infoTable, col, 0, 5);
                }
            }
        }else if(type==2||type==3){
            for(int col = 0;col<7;col++){
                if(col>2){
                    xwpfHelperTable.mergeCellsHorizontal(infoTable, col, 0, 5);
                }
            }
        }else if(type==4){
            for(int col = 0;col<7;col++){
                if(col==2){
                    xwpfHelperTable.mergeCellsHorizontal(infoTable, col, 1, 2);
                    xwpfHelperTable.mergeCellsHorizontal(infoTable, col, 4, 5);
                }

                if(col>2){
                    xwpfHelperTable.mergeCellsHorizontal(infoTable, col, 0, 5);
                }
            }
        }else if(type==5){
            for(int col = 0;col<4;col++){
                if(col>1){
                    xwpfHelperTable.mergeCellsHorizontal(infoTable, col, 1, 2);
                    xwpfHelperTable.mergeCellsHorizontal(infoTable, col, 4, 5);
                }
            }
        }

        //设置表格样式
        List<XWPFTableRow> rowList = infoTable.getRows();
        for(int i = 0; i < rowList.size(); i++) {
            XWPFTableRow infoTableRow = rowList.get(i);
            List<XWPFTableCell> cellList = infoTableRow.getTableCells();
            for(int j = 0; j < cellList.size(); j++) {
                XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
                cellParagraph.setAlignment(ParagraphAlignment.CENTER);
                XWPFRun cellParagraphRun = cellParagraph.createRun();
                cellParagraphRun.setFontSize(12);
                //设置背景颜色
                CTTcPr tcpr = cellList.get(j).getCTTc().addNewTcPr();

                if(type==1){
                    if((i<7&&j%2==0)||(i>6&&i%2==1)){
                        setBackColor(tcpr,"D7D7D7");
                    }
                }else if(type==2 || type==3){
                    if((i<3&&j%2==0)||(i>2&&i%2==1)){
                        setBackColor(tcpr,"D7D7D7");
                    }
                }else if(type==4){
                    if((i<2&&j%2==0)||(i>2&&i%2==1)){
                        setBackColor(tcpr,"D7D7D7");
                    }else if(i==2&&(j==0||j==3)){
                        setBackColor(tcpr,"D7D7D7");
                    }
                }else if(type==5){
                    if(i<2&&j%2==0) {
                        setBackColor(tcpr, "D7D7D7");
                    }else if((i==2||i==3)&&(j==0||j==3)){
                        setBackColor(tcpr,"D7D7D7");
                    }
                }
            }
        }
        //设置表格高度
        xwpfHelperTable.setTableHeight(infoTable, 100, STVerticalJc.CENTER);
    }

    /**
     * 设置单元格背景颜色
     * @param tcpr
     */
    public void setBackColor(CTTcPr tcpr,String color){
        CTShd ctshd = tcpr.addNewShd();
        ctshd.setColor("auto");
        ctshd.setVal(STShd.CLEAR);
        ctshd.setFill(color);
    }

    /**
     * 往表格中填充数据
     * @param dataList
     * @param document
     * @throws IOException
     * @Author Huangxiaocong 2018年12月16日
     */
    public void exportCheckWord(Map<String, Object> dataList, XWPFDocument document,String contextPath) throws IOException {
        XWPFParagraph paragraph = document.getParagraphArray(0);
        XWPFRun titleFun = paragraph.getRuns().get(0);
        titleFun.setText(String.valueOf(dataList.get("TITLE")));
        List<List<Object>> tableData = (List<List<Object>>) dataList.get("TABLEDATA");
        XWPFTable table = document.getTableArray(0);
        fillTableData(table, tableData,contextPath);
    }
    /**
     * 往表格中填充数据
     * @param table
     * @param tableData
     * @Author Huangxiaocong 2018年12月16日
     */
    public void fillTableData(XWPFTable table, List<List<Object>> tableData,String contextPath) {
        List<XWPFTableRow> rowList = table.getRows();
        for(int i = 0; i < tableData.size(); i++) {
            List<Object> list = tableData.get(i);
            List<XWPFTableCell> cellList = rowList.get(i).getTableCells();
            for(int j = 0; j < list.size(); j++) {
                XWPFParagraph cellParagraph = cellList.get(j).getParagraphArray(0);
                XWPFRun cellParagraphRun = cellParagraph.getRuns().get(0);
                //cellParagraphRun.setText(String.valueOf(list.get(j)));

                if(type==5){
                    /*if(i==2&&j==1){
                        if(list.get(j)!=null&&!"".equals(list.get(j))) {
                            xwpfHelper.appendExternalHyperlink(contextPath + list.get(j), "点击下载", cellParagraph);
                        }
                    }else{*/
                        cellParagraphRun.setText(String.valueOf(list.get(j)));
                    //}
                }else{
                    cellParagraphRun.setText(String.valueOf(list.get(j)));
                }
            }
        }


    }
}
